package com.ch.tools.client.simulator.controller;


import com.ch.ms.dto.MapleServerDto;
import com.ch.ms.dto.MapleWorldServerDto;
import com.ch.ms.server.packet.LoadCharactersPacket;
import com.ch.tools.client.MapleClient;
import com.ch.tools.client.simulator.LoginLauncher;
import com.ch.tools.client.simulator.observable.Observables;
import com.ch.tools.client.simulator.observer.MapleWorldServersObserver;
import com.ch.tools.client.simulator.view.LoginLauncherView;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.ResourceBundle;

@FXMLController
@Slf4j
public class ServerListViewController implements Initializable {

    private final MapleClient mapleClient;


    @FXML
    private ListView<MapleServerDto> serverListView;

    @FXML
    private Button selectButton;


    @FXML
    private Button cancelButton;


    @Autowired
    public ServerListViewController(MapleClient mapleClient) {
        this.mapleClient = mapleClient;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        LoginLauncher.getStage().setTitle("选择服务器");
        Observables.getMapleWorldServerPacketObservable().addObserver(new MapleWorldServersObserver(this));//给example这个被观察者添加观察者，允许添加多個观察者

        serverListView.setCellFactory((ListView<MapleServerDto> param) -> {
            return new ListCell<MapleServerDto>() {
                @Override
                protected void updateItem(MapleServerDto item, boolean empty) {
                    super.updateItem(item, empty);
                    if (!empty && (item != null)) {
                        setText(item.getName());
                    } else {
                        setText(null);
                    }
                }
            };
        });
    }


    private MapleServerDto selectedWorldServer;  //玩家选择世界服务器
    private MapleServerDto selectedChannelServer; //玩家选择的频道服务器


    @FXML
    public void onSelectButtonAction() {

        MapleServerDto mapleServerDto = serverListView.getSelectionModel().getSelectedItem();

        if (mapleServerDto instanceof MapleWorldServerDto) {
            Observables.getMapleWorldServerPacketObservable().setMapleServers(((MapleWorldServerDto) mapleServerDto).getChannels());
            this.selectedWorldServer = mapleServerDto;
            log.info("玩家选择了世界服务器 [{}]", selectedWorldServer);
        } else {
            this.selectedChannelServer = mapleServerDto;
            log.info("玩家选择了频道服务器 [{}]", selectedChannelServer);


            //选择完频道服务器后读取账号角色信息
            mapleClient.send(new LoadCharactersPacket());
        }
    }

    @FXML
    public void onCancelButtonButtonAction() {
        LoginLauncher.showView(LoginLauncherView.class);
    }


    public ListView<MapleServerDto> getServerListView() {
        return serverListView;
    }
}
